import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../provide/shopping_cart.dart';
import 'package:provide/provide.dart';
class CartCount extends StatelessWidget {
  final  item;

  const CartCount({Key key, this.item}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(162),
      margin: EdgeInsets.only(top: 20),
      child: Row(
        children: <Widget>[
          _buildReduceBtn(context),
          SizedBox(width: ScreenUtil().setWidth(10)),
          _buildCartCount(context),
          SizedBox(width: ScreenUtil().setWidth(10)),
          _buildAddBtn(context),
        ],
      ),
    );
  }

  //减少商品数量按钮
  Widget _buildReduceBtn(context) {
    return InkWell(
      onTap: () {
        Provide.value<ShoppingCartProvide>(context).addOrReduceAction(item,"remove");
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenUtil().setWidth(35),
        height: ScreenUtil().setHeight(35),
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: Icon(
          Icons.remove,
          size: 16,
          color: item.count>1?Colors.black:Colors.black12,
        ),
      ),
    );
  }

  //商品数量
  Widget _buildCartCount(context) {
    return Container(
      alignment: Alignment.center,
      width: ScreenUtil().setWidth(70),
      height: ScreenUtil().setHeight(35),
      color: Colors.grey[100],
      child: Text("${item.count}"),
    );
  }

  //增加商品数量的按钮
  Widget _buildAddBtn(context) {
    return InkWell(
      onTap: () {
        Provide.value<ShoppingCartProvide>(context).addOrReduceAction(item,"add");
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenUtil().setWidth(35),
        height: ScreenUtil().setHeight(35),
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: Icon(
          Icons.add,
          size: 16,
        ),
      ),
    );
  }
}
